package com.xsh.gls.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsh.gls.pojo.Company;
import com.xsh.gls.pojo.Transfer;
import com.xsh.gls.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author shkstart
 * @date 2019/1/23 - 15:11
 */
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;


    /*查询合作公司,将其分页显示*/
    @RequestMapping("/showCompany")
    public Object select(@RequestParam(required = false)String cName,@RequestParam(required = false)String pName,@RequestParam(required = false)Integer cType,@RequestParam(required = false)String pPhone, @RequestParam(required = false)String cAddress, @RequestParam(required = false)String pageNum) throws UnsupportedEncodingException {
        System.out.println(cName+"\t"+pName+"\t"+cType+"\t"+cAddress+"\t"+pPhone);
        List<Company> companyList=null;
        Integer _pageNum=1;
        if(pageNum!=null&&!pageNum.equals("")){
            _pageNum=Integer.parseInt(pageNum);
        }
        PageHelper.startPage(_pageNum,5, "ID desc");
        companyList = companyService.getByAll(cName, cType, pName, cAddress, pPhone);
        PageInfo<Company> page = new PageInfo<Company>(companyList);

        return page;

    }



    /*添加合作公司*/
    @RequestMapping("/addCompany")
    public Transfer select(@RequestParam(required = false)String cName, @RequestParam(required = false)Integer cType, @RequestParam(required = false) String pName,
                           @RequestParam(required = false)String pPhone,@RequestParam(required = false)String cAddress) throws UnsupportedEncodingException {
        Company company = new Company();
        Transfer transfer = new Transfer();
        Company company2 = companyService.selectByNT(cName, cType);
        if (company2 != null){
            transfer.setMessage("no");
        }else {
            company.setcName(cName);
            company.setpName(pName);
            company.setpPhone(pPhone);
            company.setcAddress(cAddress);
            company.setcType(cType);
            if (companyService.insert(company)){
                transfer.setMessage("yes");
            }else {
                transfer.setMessage("error");
            }
        }
        return transfer;
    }

    /*跳转到修改合作公司页面*/
    @RequestMapping("/showUpdateCompany")
    public Object select(@RequestParam(required = false)Integer id) throws UnsupportedEncodingException {
        System.out.println(id);
        Company company = companyService.selectAll(id);
        System.out.println(company.getcName()+"\t"+company.getpName());
        return company;
    }


    /*修改合作公司*/
    @RequestMapping("/updateCompany")
    public Transfer updateCompany(@RequestParam(required = false)Integer id,@RequestParam(required = false)String cName, @RequestParam(required = false)Integer cType, @RequestParam(required = false) String pName,
                           @RequestParam(required = false)String pPhone,@RequestParam(required = false)String cAddress) throws UnsupportedEncodingException {
        Transfer transfer = new Transfer();
        Company company = companyService.selectAll(id);
        if (company == null){
            transfer.setMessage("no");
        }else {
            company.setcName(cName);
            company.setpName(pName);
            company.setcAddress(cAddress);
            company.setcType(cType);
            company.setpPhone(pPhone);
            if (companyService.updateByPrimaryKey(company)){
                transfer.setMessage("yes");
            }else {
                transfer.setMessage("no");
            }
        }
        return transfer;
    }

    /*删除合作公司*/
    @RequestMapping("/deleteCompany")
    public Transfer delete(@RequestParam(required = false)Integer id) throws UnsupportedEncodingException {
        Transfer transfer = new Transfer();
        Company company = companyService.selectAll(id);
        if (company == null){
            transfer.setMessage("no");
        }else {
            if (companyService.deleteByPrimaryKey(company.getId())){
                transfer.setMessage("yes");
            }
        }
        return transfer;
    }
}
